package com.ruoyi.hotel.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.hotel.domain.HmRoom;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.hotel.domain.bo.HmRoomTypeBo;
import com.ruoyi.hotel.domain.vo.HmRoomTypeVo;
import com.ruoyi.hotel.domain.HmRoomType;
import com.ruoyi.hotel.mapper.HmRoomTypeMapper;
import com.ruoyi.hotel.service.IHmRoomTypeService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author ruoyi
 * @date 2023-11-01
 */
@RequiredArgsConstructor
@Service
public class HmRoomTypeServiceImpl implements IHmRoomTypeService {

    private final HmRoomTypeMapper baseMapper;

    /**
     * 查询【请填写功能名称】
     */
    @Override
    public HmRoomTypeVo queryById(String roomType){
        return baseMapper.selectVoById(roomType);
    }

    /**
     * 查询【请填写功能名称】列表
     */
    @Override
    public TableDataInfo<HmRoomTypeVo> queryPageList(HmRoomTypeBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<HmRoomType> lqw = buildQueryWrapper(bo);
        Page<HmRoomTypeVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询【请填写功能名称】列表
     */
    @Override
    public List<HmRoomTypeVo> queryList(HmRoomTypeBo bo) {
        LambdaQueryWrapper<HmRoomType> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<HmRoomType> buildQueryWrapper(HmRoomTypeBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<HmRoomType> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getRoomCount() != null, HmRoomType::getRoomCount, bo.getRoomCount());
        return lqw;
    }

    /**
     * 新增【请填写功能名称】
     */
    @Override
    public Boolean insertByBo(HmRoomTypeBo bo) {
        HmRoomType add = BeanUtil.toBean(bo, HmRoomType.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setRoomType(add.getRoomType());
        }
        return flag;
    }

    /**
     * 修改【请填写功能名称】
     */
    @Override
    public Boolean updateByBo(HmRoomTypeBo bo) {
        HmRoomType update = BeanUtil.toBean(bo, HmRoomType.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(HmRoomType entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除【请填写功能名称】
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }


    @Override
    public List<HmRoomType> getRoomType(){
        QueryWrapper<HmRoomType> queryWrapper=new QueryWrapper<>();
        queryWrapper.gt("room_count",0);
        List<HmRoomType> list=baseMapper.selectList(queryWrapper);
        return list;
    }

    @Override
    public void addRoomCount(HmRoom room){
        QueryWrapper<HmRoomType> qw=new QueryWrapper<>();
        qw.eq("room_type",room.getRoomType());
        HmRoomType hmRoomType=baseMapper.selectOne(qw);
        Long temp=hmRoomType.getRoomCount()+1;
        hmRoomType.setRoomCount(temp);
        baseMapper.update(hmRoomType,qw);
    }

    @Override
    public void minusRoomCount(HmRoom room){
        QueryWrapper<HmRoomType> qw=new QueryWrapper<>();
        qw.eq("room_type",room.getRoomType());
        HmRoomType hmRoomType=baseMapper.selectOne(qw);
        Long temp=hmRoomType.getRoomCount()-1;
        hmRoomType.setRoomCount(temp);
        baseMapper.update(hmRoomType,qw);
    }



}
